package ServiceProvider;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Method;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;

import Middleware.Middleware;

public class SPGUI extends JFrame {

	private JPanel contentPane;
	private JComboBox<String> comboSPBox;
	private SP sp;
	private JTextPane textOutput;
	private StringBuilder outputText;
	private Middleware middle;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SPGUI frame = new SPGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public SPGUI() {
		setTitle("SP");
		
		Method logMethod = null;
		try {
			logMethod = this.getClass().getDeclaredMethod("Log", String.class);
			
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		}
		sp = new SP(logMethod, this);
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 600, 353);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		comboSPBox = new JComboBox<String>();
		comboSPBox.setBounds(10, 11, 211, 22);
		contentPane.add(comboSPBox);
		
		JButton buttonLoad = new JButton("Load");
		buttonLoad.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				loadCert();
			}
		});
		buttonLoad.setBounds(231, 11, 91, 23);
		contentPane.add(buttonLoad);
		
		textOutput = new JTextPane();
		textOutput.setEditable(false);
		
		JScrollPane jsp = new JScrollPane(textOutput);
		contentPane.add(jsp);
		jsp.setBounds(10, 60, 570, 250);
		
		JButton btnConnect = new JButton("Connect");
		btnConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				connectToCard();
			}
		});
		btnConnect.setBounds(492, 11, 91, 23);
		contentPane.add(btnConnect);
		
		for(String s : sp.listOfCerts) {
			comboSPBox.addItem(s);
		}
		
		outputText = new StringBuilder("---Communication with Card/middleware---\n \n");
		sp.selectedCertID = comboSPBox.getSelectedIndex();
		outputText.append(sp.getCertInfo(sp.listOfCerts[sp.selectedCertID]));
		textOutput.setText(outputText.toString());
		
		JLabel lblSpLoggingInformation = new JLabel("SP logging information:");
		lblSpLoggingInformation.setBounds(10, 44, 123, 14);
		contentPane.add(lblSpLoggingInformation);
	}
	
	public void Log(String s) {
		outputText.append(s);
		this.textOutput.setText(outputText.toString());
	}
	
	public void connectToCard() {
		outputText.append("- Trying to connect to JavaCard\n");
		textOutput.setText(outputText.toString());
		
		Thread mwThread = new Thread(sp);
		mwThread.start();
	}
	
	public void loadCert() {
		sp.selectedCertID = comboSPBox.getSelectedIndex();
		outputText.append(sp.getCertInfo(sp.listOfCerts[sp.selectedCertID]));
		textOutput.setText(outputText.toString());
	}
}
